package website.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import website.constants.Code;
import website.domain.*;
import website.mapper.CommentMapper;
import website.mapper.LinkMapper;
import website.mapper.UserMapper;
import website.service.CommentService;
import website.service.LinkService;
import website.service.UserService;
import website.service.WebService;
import website.utils.NowDateUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private UserMapper userMapper;

    @Autowired
    private WebService webService;


    /**
     * 发布评论
     * @param comment
     * @return
     */
    @Override
    public ResModel issue(Comment comment) {
        if (comment.getUid()!=null){
            comment.setCreateTime(NowDateUtils.getNowDate());
            boolean save = save(comment);
            //更新网站评论量
            webService.update().setSql("comments = comments + 1").update();
            return ResModel.success(Code.ISSUE,save);
        }
        return ResModel.error(Code.UN_ISSUE);
    }

    /**
     * 查询评论
     * @param current
     * @param size
     * @param rid
     * @return
     */
    @Override
    public ResModel<IPage<Comment>> getByPage(Integer current, Integer size, Integer rid) {
        Page<Comment> page = new Page<>(current, size);
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(true,Comment::getRid,rid);
        wrapper.eq(true,Comment::getParentId,0);
        Page<Comment> commentPage = commentMapper.selectPage(page, wrapper);
        List<Comment> commentList = commentPage.getRecords();
        for (Comment comment : commentList) {
            User user = userMapper.getByUid(comment.getUid());
            comment.setAvatar(user.getAvatar());
            comment.setNickname(user.getNickname());
            List<Comment> replyCommentList = commentMapper.getByRidAndParentId(comment.getRid(), comment.getId());
            comment.setReplyCount(replyCommentList.size());
        }
        return ResModel.success(Code.SUCCESS,commentPage);
    }

    /**
     * 回复评论
     * @param comment
     * @return
     */
    @Override
    public ResModel reply(Comment comment) {
        if(comment.getParentId()!=null){
            comment.setUid(comment.getReplyId());
            comment.setReplyId(0);
            comment.setCreateTime(NowDateUtils.getNowDate());
            boolean save = save(comment);
            //更新网站评论量
            webService.update().setSql("comments = comments + 1").update();
            return ResModel.success(Code.SUCCESS,save);
        }
        return ResModel.error(Code.FAIL);
    }

    /**
     * 查询评论的回复
     * @param rid
     * @param id
     * @return
     */
    @Override
    public ResModel<List<Comment>> listReply(Integer rid, Integer id) {
        List<Comment> commentList = commentMapper.getByRidAndParentId(rid, id);
        for (Comment comment : commentList) {
            User user = userMapper.getByUid(comment.getUid());
            comment.setAvatar(user.getAvatar());
            comment.setNickname(user.getNickname());
        }
        return ResModel.success(Code.SUCCESS,commentList);
    }

    /**
     * 查询评论总数
     * @param rid
     * @return
     */
    @Override
    public ResModel getCount(Integer rid) {
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(true,Comment::getRid,rid);
        return ResModel.success(Code.SUCCESS,count(wrapper));
    }

    /**
     * 分页查询评论信息
     * @param current
     * @param size
     * @return
     */
    @Override
    public ResModel<IPage<Comment>> getByList(Integer current, Integer size) {
        Page<Comment> page = new Page<>(current, size);
        Page<Comment> messagePage = commentMapper.selectPage(page, null);
        return ResModel.success(Code.SUCCESS,messagePage);
    }

    /**
     * 删除评论信息
     * @param id
     * @return
     */
    @Override
    public ResModel delComment(Integer id) {
        if (id!=null){
            boolean b = removeById(id);
            //更新网站评论量
            webService.update().setSql("comments = comments - 1").update();
            return ResModel.success(Code.DELETE,b);
        }
        return ResModel.error(Code.FAIL);
    }
}
